import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { DefectDefineModel } from '#/api/quality/models';
import type { ReworkDocModel } from '#/api/rework/models';

import { getOperationListApi } from '#/api/process/operation';
import { getDefectDefineListApi } from '#/api/quality/defectDefine';

/** 获取表格查询表单的字段配置。 */
export function useSearchSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      componentProps: {
        allowClear: true,
      },
      fieldName: 'sn',
      label: '零件码',
    },
  ];
}

/**
 * 获取编辑表单的字段配置。
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'sn',
      disabled: true,
      label: '零件码',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        showSearch: true,
        mode: 'multiple',
        optionFilterProp: 'label',
        afterFetch: (data: DefectDefineModel[]) => {
          // TODO: 采用树结构，参考 "点检标件-工位多选"
          return data.map((item) => ({
            label: `${item.defectCode} - ${item.defectName}`,
            value: item.id,
          }));
        },
        api: getDefectDefineListApi,
      },
      fieldName: 'defectIds',
      label: '缺陷问题',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'ngOperationCode',
      disabled: true,
      label: '不合格工序',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        showSearch: true,
        optionFilterProp: 'label',
        labelField: 'opName',
        valueField: 'id',
        api: getOperationListApi,
        immediate: false,
      },
      fieldName: 'targetOperationId',
      label: '重新上线工序',
      rules: 'required',
    },
    {
      component: 'Textarea',
      componentProps: {
        showCount: true,
        maxlength: 128,
      },
      fieldName: 'remark',
      label: '返工原因',
    },
  ];
}

/**
 * 获取表格列配置。
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: OnActionClickFn<ReworkDocModel>,
): VxeTableGridOptions<ReworkDocModel>['columns'] {
  return [
    { type: 'seq', width: 50 },
    { field: 'sn', title: '零件码', width: 220 },
    {
      field: 'ngOperationCode',
      title: '不合格工序',
      width: 100,
      cellRender: {
        attrs: {
          color: 'red',
        },
        name: 'CellTag',
      },
    },
    {
      field: 'targetOperationCode',
      title: '重新上线工序',
      width: 110,
      cellRender: {
        attrs: {
          color: 'yellow',
        },
        name: 'CellTag',
      },
    },
    {
      field: 'defects',
      title: '缺陷问题',
      width: 120,
      formatter: ({ cellValue }) => {
        return cellValue.map((e: any) => e.defectName).join('、');
      },
    },
    {
      field: 'status',
      title: '返工状态',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: [
          { color: 'orange', label: '已创建', value: 1 },
          { color: 'green', label: '返工中', value: 2 },
          { color: 'blue', label: '已完成', value: 3 },
          { color: 'red', label: '取消', value: 9 },
        ],
      },
    },
    { field: 'remark', title: '返工原因', minWidth: 100 },
    {
      cellRender: {
        attrs: {
          nameField: 'sn',
          nameTitle: '零件码',
          onClick: onActionClick,
        },
        props: {
          disabled: (params: any) => params.status !== 1,
        },
        name: 'CellOperation',
        options: ['edit', { code: 'cancel', text: '取消', confirm: true }],
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 100,
    },
  ];
}
